package bintree.heap.leetcode;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 1046. 最后一块石头的重量
 * @author haomin
 * @date 2022/03/23 20:34
 **/
public class Num1046_LastStoneWeight {
    public int lastStoneWeight(int[] stones) {
        //优先级队列-大根堆
        Queue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        //入队
        for(int i:stones){
            queue.offer(i);
        }

        //入队(y-x)
        while (queue.size() > 1) {
            int y = queue.poll();
            int x = queue.poll();
            queue.offer(y - x);
        }

        //剩下的值就是最后的石头重量
        int ret = queue.poll();
        return ret;
    }
}